<?php


namespace app\admin\controller\jizhan;


use app\admin\model\HtnsBasestationStore;
use app\admin\model\HtnsOrder;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use jianyan\excel\Excel;
use think\App;
use think\facade\Cache;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="基站-订单数据统计")
 */
class StoreOrderSum extends AdminController
{

    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new HtnsOrder();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index($export = 0)
    {
        $storeId = session('admin')['store_id'];//当前用户门店id
        $htnsBasestationStore = new HtnsBasestationStore();
        $storeList            = $htnsBasestationStore->getStoreNameZz($storeId);
        if ($this->request->isAjax() || $export) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            // 自定义搜索条件解析
            $whereArr = [];
            $filter   = input('filter');
            $filter = htmlspecialchars_decode($filter);// html特殊字符转化&quot;转换为'
            if ($filter) {
                $filterArr = json_decode($filter, true);

                if (isset($filterArr['payTime'])) {
                    $time       = explode(' - ', $filterArr['payTime']);
                    $whereArr[] = ['pay_time', 'between', [strtotime($time[0]), strtotime($time[1])]];
                }
                if (isset($filterArr['store_id'])) {
                    $whereArr[] = ['store_id', '=', $filterArr['store_id']];
                }
            }
            if($storeId){
                $whereArr[] = ['store_id','=',$storeId];
            }
            if ($export) {
                $page  = 1;
                $limit = 10000;
            }
            $count = 1;
            $list = [];

            $whereArr[] = ['paid','=',1];
            $whereArr[] = ['refund_status','in',[0,3]];
            $whereArr[] = ['is_del','=',0];
            $whereArr[] = ['is_system_del','=',0];
            $payPriceSum = $this->model->where($whereArr)->sum('pay_price');// 已支付订单总金额数
            $listv['payPriceSum'] = $payPriceSum;
            $orderNum = $this->model->where($whereArr)->count('id');// 订单总数量
            $listv['orderNum'] = $orderNum;

            $offlineWhereArr = $whereArr;
            $offlineWhereArr[] = ['order_types','=','store'];
            $offlinePayPrice = $this->model->where($offlineWhereArr)->sum('pay_price');// 线下订单总销售额
            $listv['offlinePayPrice'] = $offlinePayPrice;

            $onlineWhereArr = $whereArr;
            $onlineWhereArr[] = ['order_types','=','normal'];
            $onlinePayPrice = $this->model->where($onlineWhereArr)->sum('pay_price');// 线上订单总销售额
            $listv['onlinePayPrice'] = $onlinePayPrice;

            $orderRate = '0%';
            if($onlinePayPrice){
                $orderRate = bcdiv($offlinePayPrice , $onlinePayPrice, 4);
                $orderRate = ($orderRate * 100) .'%';
            }
            $listv['orderRate'] = $orderRate;

            $list[] = $listv;
            // 导出
            if ($export) {
                return $list;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('storeList', $storeList);
        return $this->fetch();
    }
}